package org.raymond.iworks.study.basic.structure.stack;

/**
 * @author raymond
 * @version V1.0
 * @Description:
 * 实现思路分析:
 * 1, 使用数组来模拟栈
 * 2, 定义一个top来表示栈顶,初始化为-1.
 * 3, 入栈的操作，当有数据加入到栈时, top++; stack[top]=data;
 * 4, 出栈的操作, val=stack[top]; top--; return val;
 */
public class ArrayStack {
    private int max;
    private int top;
    private int[] array;

    public ArrayStack(int max){
        this.max = max;
        top = -1;
        array = new int[max];
    }

    public void push(int i){
        if(isFull()){
            System.err.println("栈已满");
            return;
        }
        top++;
        array[top] = i;
    }

    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("stack is empty");
        }
        int tmp = array[top];
        top--;
        return tmp;
    }
    public boolean isFull(){
        return top == max-1;
    }

    public boolean isEmpty(){
        return top == -1;
    }

    public void show(){
        if(isEmpty()){
            System.err.println("stack is empty");
            return;
        }
        for(int i=top; i>=0; i--){
            System.out.println("->"+array[i]);
        }
    }
}
